//
//  PhotoReviewController.m
//  3DModelCollector
//
//  Created by Jing Zhang on 7/20/12.
//  Copyright (c) 2012 Tencent. All rights reserved.
//

#import "PhotoReviewController.h"

@interface PhotoReviewController ()

@property (retain, nonatomic) IBOutlet UIImageView *photoView;
@property (retain, nonatomic) IBOutlet UIView *actionView;

- (IBAction)onClickRetake:(id)sender;
- (IBAction)onClickUsePhoto:(id)sender;

- (void)onTapImage:(UIGestureRecognizer*)gestureRecognizer;

@end

@implementation PhotoReviewController

@synthesize photo = _photo;
@synthesize photoInfo = _photoInfo;
@synthesize photoDelegate = _photoDelegate;

@synthesize photoView;
@synthesize actionView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.wantsFullScreenLayout = YES;
    }
    return self;
}

- (void)dealloc {
    self.photo = nil;
    self.photoInfo = nil;
    self.photoDelegate = nil;
    [photoView release];
    [actionView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (self.photo != nil) {
        CGSize size = self.photo.size;
        CGRect frame;
        if (size.width > size.height) {
            frame.size.width = self.view.frame.size.width;
            frame.size.height = size.height * frame.size.width / size.width;
            frame.origin.x = 0;
            frame.origin.y = (480 - frame.size.height) / 2;
        }
        else {
            frame.size.height = 480;
            frame.size.width = size.width * frame.size.height / size.height;
            frame.origin.x = (320 - frame.size.width) / 2;
            frame.origin.y = 0;
        }
        
        self.photoView.frame = frame;
        
        self.photoView.image = self.photo;
        
        self.photoView.userInteractionEnabled = YES;
        UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapImage:)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        [self.photoView addGestureRecognizer:tapGestureRecognizer];
        [tapGestureRecognizer release];
    }
}

- (void)viewDidUnload
{
    [self setPhotoView:nil];
    [self setActionView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - [Private methods]

- (IBAction)onClickRetake:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onClickUsePhoto:(id)sender {
    
    if (self.photoDelegate != nil) {
        [self.photoDelegate addPhoto:self.photo description:self.photoInfo];
    }
    
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

- (void)onTapImage:(UIGestureRecognizer *)gestureRecognizer {
    
    UIApplication* app = [UIApplication sharedApplication];
    if (app.statusBarHidden) {
        [app setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
        [UIView animateWithDuration:0.3f animations:^(){
            self.actionView.alpha = 1;
        }];
    }
    else {
        [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
        [UIView animateWithDuration:0.3f animations:^(){
            self.actionView.alpha = 0;
        }];
    }
}

@end
